<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{ commentInfo.evaluateCount }}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{ commentInfo.hasPictureCount }}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        href="javascript:;"
        :class="{ active: reqParams.sortField === itm.type }"
        v-for="(itm, i) in sortList"
        :key="i"
        @click="sortFn(itm.type)"
        >{{ itm.name }}</a
      >
      <xtx-check-box
        @click="loadCommentList"
        v-model="reqParams.hasPicture"
        style="margin-left: 50px"
        >是否有图</xtx-check-box
      >
    </div>
    <!-- 列表 -->
    <div class="list" v-for="itm in commentData" :key="itm.id">
      <div class="item">
        <div class="user">
          <img :src="itm.member.avatar" alt="" />
          <span>{{ itm.member.nickname }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i
              class="iconfont icon-wjx01"
              v-for="(add, i) in itm.score"
              :key="i"
            ></i>
            <i
              class="iconfont icon-wjx02"
              v-for="(add, i) in 5 - itm.score"
              :key="i"
            ></i>
            <span
              class="attr"
              v-for="itms in itm.orderInfo.specs"
              :key="itms.name"
              >{{ itms.name }}：{{ itms.nameValue }}
            </span>
          </div>
          <div class="text">
            {{ itm.content }}
          </div>
          <div class="time">
            <span>{{ itm.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ itm.praiseCount }}</span
            >
          </div>
          <GoodsCommentImg v-if="itm.pictures.length" :pictures="itm.pictures" />
        </div>
      </div>
    </div>
    <InfiLoad :isFedir="isFedir" :isLoad="isLoad" @sub-page='subPage'/>
  </div>
</template>
<script>
import { onMounted, ref, reactive } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import GoodsCommentImg from './goods-comment-img'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentImg },
  setup () {
    const route = useRoute()
    const commentInfo = ref({})
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: false, // 是否有
      sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
    })

    const sortList = [
      {
        name: '默认',
        type: null
      },
      {
        name: '最新',
        type: 'praiseCount'
      },
      {
        name: '最热',
        type: 'createTime'
      }
    ]
    const isFedir = ref(true)
    const isLoad = ref(true)
    async function subPage () {
      reqParams.page++
      isLoad.value = false
      const refs = await findCommentListByGoods(route.params.id, reqParams)
      isLoad.value = true
      if (refs.result.items.length !== 10) {
        isFedir.value = false
      }
      commentData.value = [...commentData.value, ...refs.result.items]
    }
    async function loadData () {
      const res = await findCommentInfoByGoods(route.params.id)
      commentInfo.value = res.result
    }
    const commentData = ref([])
    async function loadCommentList () {
      const refs = await findCommentListByGoods(route.params.id, reqParams)
      commentData.value = refs.result.items
    }
    function sortFn (type) {
      reqParams.sortField = type
      loadCommentList()
    }
    onMounted(() => {
      loadData()
      loadCommentList()
    })
    return {
      commentInfo,
      sortList,
      sortFn,
      commentData,
      reqParams,
      loadCommentList,
      subPage,
      isFedir,
      isLoad
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex: 1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
